﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 第八次课堂作业
{
    public partial class Form1 : Form
    {
        string multiNum = "";//用户多位数的显示
        Stack<string> final = new Stack<string>();//存储两次栈操作的第二次
        Stack<string> temp = new Stack<string>();//存储两次栈操作的第一次

        public Form1()
        {
            InitializeComponent();
        }

        //点击数字事件
        private void number_Click(object sender, EventArgs e)
        {
            //获得数字号
            string tempNum = ((Button)sender).Name;
            int number = int.Parse(tempNum.Replace("btn", ""));

            //将数字号显示
            textBoxAnswer.Text += number;
            multiNum += number;
        }

        private void operate_Click(object sender, EventArgs e)
        {
            //获得操作键的名称
            string temp = ((Button)sender).Name;
            if (temp == "btnAdd")
            {
                insertOperate('+');
            }
            else if (temp == "btnMinus")
            {
                insertOperate('-');
            }
            else if (temp == "btnTime")
            {
                insertOperate('*');
            }
            else if (temp == "btnDivide")
            {
                insertOperate('/');
            }
        }

        private void insertOperate(char v)
        {
            try
            {
                //将数字入栈
                final.Push(multiNum);
                //如果是加法或减法，执行相应操作
                if (v == '+' || v == '-')
                {
                    while (temp.Count != 0)
                    {
                        final.Push(temp.Pop());
                    }
                    temp.Push(v.ToString());
                }
                //如果是乘法或除法，执行相应操作
                else if (v == '*' || v == '/')
                {
                    //当栈空或者栈顶为加减号时再进栈
                    while (temp.Count != 0)
                    {
                        if (temp.Peek() == "*" || temp.Peek() == "/")
                        {
                            final.Push(temp.Pop());
                        }
                        else break;
                    }
                    temp.Push(v.ToString());
                }
                //如果是等号，全部进入到第二个栈中
                else if (v == '=')
                {
                    while (temp.Count > 0)
                    {
                        final.Push(temp.Pop());
                    }
                }

            }
            catch (Exception a)
            {
                MessageBox.Show(a.Message);
                myClear();
            }
            finally
            {
                textBoxAnswer.Text += v;
                multiNum = "";//num变为空
            }
        }

        //进行整体的计算
        private void calculate()
        {
            //vsFinal栈中已经是后缀表达式
            List<string> vs = final.ToList();
            vs.Reverse();
            int i = 0;
            while (vs.Count() > 1)
            {
                if (vs[i] == "+" || vs[i] == "-" || vs[i] == "*" || vs[i] == "/")
                {
                    vs[i - 2] = function(vs[i - 2], vs[i - 1], vs[i]);
                    for (int j = i - 1; j < vs.Count - 2; j++)
                    {
                        vs[j] = vs[j + 2];
                    }
                    vs.RemoveAt(vs.Count() - 2);
                    vs.RemoveAt(vs.Count() - 1);

                    i -= 2;
                }
                else
                {
                    i++;
                }
            }
            textBoxAnswer.Text = vs[0];
        }

        //进行两个数字间的计算
        public string function(string v1, string v2, string v3)
        {
            double n1 = Convert.ToDouble(v1);
            double n2 = Convert.ToDouble(v2);
            double n = 0;
            if (v3 == "+")
            {
                n = n1 + n2;
            }
            else if (v3 == "-")
            {
                n = n1 - n2;
            }
            else if (v3 == "*")
            {
                n = n1 * n2;
            }
            else if (v3 == "/")
            {
                if (n2 == 0)
                {
                    throw new Exception("除数不能为0");
                }
                n = n1 / n2;
            }
            return n.ToString();
        }

        //进行清空操作
        private void myClear()
        {
            textBoxAnswer.Text = "";
            multiNum = "";
            final.Clear();
            temp.Clear();
        }

        private void btnEqual_Click(object sender, EventArgs e)
        {
            insertOperate('=');
            try
            {
                calculate();
            }
            catch (Exception a)
            {
                MessageBox.Show(a.Message);
                myClear();
            }
            finally
            {
                final.Clear();
                temp.Clear();
            }
        }

        private void btnClean_Click(object sender, EventArgs e)
        {
            myClear();
        }
    }
}
